package com.keson.guard.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.keson.guard.GuardApplication;
import com.keson.guard.MainActivity;
import com.keson.guard.util.CmdUtil;

public class SetupBroadcast extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                Intent intentMainActivity = new Intent(context, MainActivity.class);
                intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentMainActivity);
            }else {
                Intent intentMainActivity = new Intent(context, GuardApplication.class);
                intentMainActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intentMainActivity);
            }
            //Toast.makeText(context, "开机完毕~", Toast.LENGTH_LONG).show();
        }else if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction())){
            if("com.keson.eq".equals(intent.getData().getEncodedSchemeSpecificPart())){
                CmdUtil.execRootCmd("am start -n com.keson.eq/.StartActivity");
            }
        }else if("com.keson.eq.restartApp".equals(intent.getAction())){
            CmdUtil.execRootCmd("am force-stop com.keson.eq");
            CmdUtil.execRootCmd("am start -n com.keson.eq/.StartActivity");
        }
    }
}
